//
//  ZZInputMainView.m
//  ZZInputView
//
//  Created by 郑程旭 on 2022/2/19.
//  Copyright © 2022 qq282424020. All rights reserved.
//

#import "ZZInputMainView.h"
#import "Masonry.h"

@interface ZZInputAccessoryView : UIView

@property (nonatomic, strong) UIButton *soundBtn;
@property (nonatomic, strong) UIButton *takePhotoBtn;
@property (nonatomic, strong) UIButton *selectPhotoBtn;
@property (nonatomic, copy) void(^didClickMenuBlock)(NSInteger index); //1.语音 2.相机 3.相册

@end

@implementation ZZInputAccessoryView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 1)];
        lineView.backgroundColor = [UIColor colorWithRed:230/255. green:230/255. blue:230/255. alpha:1];
        [self addSubview:lineView];
        
        NSArray *imagesArr = @[@"life_ipt_sound",@"life_ipt_tk_p",@"life_ipt_slt_p"];
        for (int i = 0; i < imagesArr.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setImage:[UIImage imageNamed:imagesArr[i]] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(inputBtnsAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = 10001+i;
            [self addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.mas_equalTo(55);
                make.top.mas_equalTo(0);
                make.left.mas_equalTo(55*i);
            }];
        }
    }
    return self;
}

- (void)inputBtnsAction:(UIButton *)sender {
    if (_didClickMenuBlock) {
        _didClickMenuBlock(sender.tag - 10000);
    }
}

@end


@interface ZZInputMainView () <UITextViewDelegate>

@property (nonatomic, strong) ZZInputAccessoryView *inputAccessoryView;

@end

@implementation ZZInputMainView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];

        [self configSubviews];
    }
    return self;
}

#pragma mark - Subviews
- (void)configSubviews {
    [self addSubview:self.sendBtn];
    [self addSubview:self.textGrayView];
    [self addSubview:self.textView];
    [self addSubview:self.placeholderLbl];

}

- (UIButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendBtn.frame = CGRectMake(CGRectGetWidth(self.frame)-67-17, CGRectGetHeight(self.frame)-10-36, 67, 36);
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn setTitleColor:[UIColor colorWithRed:102/255. green:102/255. blue:102/255. alpha:1] forState:UIControlStateNormal];
        _sendBtn.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:239/255. alpha:1];
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:19];
        _sendBtn.userInteractionEnabled = NO;
        _sendBtn.layer.cornerRadius = 18;
        _sendBtn.layer.masksToBounds = YES;
    }
    return _sendBtn;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(17, 10, CGRectGetWidth(self.frame)-67-17-17-10, 36)];
        _textView.backgroundColor = [UIColor clearColor];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:17];
        _textView.textContainerInset = UIEdgeInsetsMake(7, 10, 0, 0);
        _textView.tintColor = [UIColor colorWithRed:1/255. green:200/255. blue:75/255. alpha:1];
        _textView.textColor = [UIColor colorWithRed:102/255. green:102/255. blue:102/255. alpha:1];
        _textView.inputAccessoryView = self.inputAccessoryView;
    }
    return _textView;
}

- (UILabel *)placeholderLbl {
    if (!_placeholderLbl) {
        _placeholderLbl = [[UILabel alloc] initWithFrame:CGRectMake(31.5, 9.5, 120, 36)];
        _placeholderLbl.font = [UIFont systemFontOfSize:17];
        _placeholderLbl.textColor = [UIColor colorWithRed:102/255. green:102/255. blue:102/255. alpha:1];
        _placeholderLbl.text = @"写评论...";
    }
    return _placeholderLbl;
}

- (ZZInputAccessoryView *)inputAccessoryView {
    if (!_inputAccessoryView) {
        _inputAccessoryView = [[ZZInputAccessoryView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 52)];
        __weak ZZInputMainView *weakSelf = self;
        _inputAccessoryView.didClickMenuBlock = ^(NSInteger index) {
            if (index == 1) {
                if ([weakSelf.delegate respondsToSelector:@selector(inputMainViewSelectRecordVoice)]) {
                    [weakSelf.delegate inputMainViewSelectRecordVoice];
                }
            } else if (index == 2) {
                if ([weakSelf.delegate respondsToSelector:@selector(inputMainViewSelectTakePhoto)]) {
                    [weakSelf.delegate inputMainViewSelectTakePhoto];
                }
            } else if (index == 3) {
                if ([weakSelf.delegate respondsToSelector:@selector(inputMainViewSelectPhotoLibrary)]) {
                    [weakSelf.delegate inputMainViewSelectPhotoLibrary];
                }
            }
        };
    }
    return _inputAccessoryView;
}

- (UIView *)textGrayView {
    if (!_textGrayView) {
        _textGrayView = [[UIView alloc] initWithFrame:CGRectMake(17, 10, CGRectGetWidth(self.frame)-67-17-17-10, 36)];
        _textGrayView.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:239/255. alpha:1];
        _textGrayView.layer.cornerRadius = 18;
        _textGrayView.layer.masksToBounds = YES;
        _textGrayView.userInteractionEnabled = YES;
        UITapGestureRecognizer *notHiddenTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(notHiddenAction)];
        [_textGrayView addGestureRecognizer:notHiddenTapGes];
    }
    return _textGrayView;
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length > 0) {
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sendBtn.backgroundColor = [UIColor colorWithRed:1/255. green:200/255. blue:75/255. alpha:1];
        _sendBtn.userInteractionEnabled = NO;
        _placeholderLbl.hidden = YES;
    } else {
        [_sendBtn setTitleColor:[UIColor colorWithRed:102/255. green:102/255. blue:102/255. alpha:1] forState:UIControlStateNormal];
        _sendBtn.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:239/255. alpha:1];
        _sendBtn.userInteractionEnabled = YES;
        _placeholderLbl.hidden = NO;
    }
    
    //TODO: 赋值文字内容
    
    CGRect frame = textView.frame;
    CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(CGRectGetWidth(textView.frame), MAXFLOAT)];
    CGFloat height = sizeToFit.height;
    if (height < 36) {
        height = 36;
    } else if (height > 108) {
        height = 108;
    } else {
        height = height+7;
    }

    self.frame = CGRectMake(0, CGRectGetMinY(self.frame)-(height-frame.size.height), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)+height-frame.size.height);
    
    CGRect whiteFrame = _textGrayView.frame;
    whiteFrame.size.height = height;
    
    //    if (self.image) {
    //        whiteFrame.size.height = height+self.imageHeight;
    //    } else {
    //        whiteFrame.size.height = height;
    //    }

    _textGrayView.frame = whiteFrame;

    _sendBtn.frame = CGRectMake(CGRectGetWidth(self.frame)-67-17, CGRectGetHeight(self.frame)-10-36, 67, 36);
    
    frame.size.height = height;
    textView.frame = frame;

    if ([_delegate respondsToSelector:@selector(inputMainViewHeightChanged:)]) {
        [_delegate inputMainViewHeightChanged:CGRectGetHeight(self.frame)];
    }
    
}

#pragma mark - Actions
- (void)notHiddenAction {}
        



@end



